<template>
  <div class="container">
    <div class="form-wrapper reset-password">
      <form @submit.prevent="resetPassword">
        <div class="form-header">
          <h2>重置密码</h2>
          <p class="subtitle">请输入重置码和新密码</p>
        </div>

        <div class="input-group">
          <input
            type="text"
            required
            v-model="userId"
            :class="{ 'has-value': userId }"
          >
          <label>用户ID</label>
          <span class="input-line"></span>
        </div>

        <div class="input-group">
          <input
            type="text"
            required
            v-model="resetCode"
            :class="{ 'has-value': resetCode }"
          >
          <label>重置码</label>
          <span class="input-line"></span>
        </div>

        <div class="input-group">
          <input
            :type="showPassword ? 'text' : 'password'"
            required
            v-model="newPassword"
            :class="{ 'has-value': newPassword }"
          >
          <label>新密码</label>
          <span class="input-line"></span>
          <button
            type="button"
            class="toggle-password"
            @click="showPassword = !showPassword"
          >
            {{ showPassword ? '隐藏密码' : '显示密码' }}
          </button>
        </div>

        <div class="password-strength" v-if="newPassword">
          <div class="strength-bar">
            <div
              class="strength-fill"
              :style="{ width: passwordStrength + '%' }"
              :class="strengthClass"
            ></div>
          </div>
          <span class="strength-text">{{ strengthText }}</span>
        </div>

        <button
          type="submit"
          class="submit-btn"
          :disabled="isLoading || !isValidPassword"
        >
          <span v-if="!isLoading">重置密码</span>
          <span v-else class="loading-spinner"></span>
        </button>

        <div class="form-footer">
          <router-link to="/login" class="back-link">
            <span class="arrow">←</span> 返回登录
          </router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

export default {
  setup() {
    const router = useRouter();
    const userId = ref('');
    const resetCode = ref('');
    const newPassword = ref('');
    const showPassword = ref(false);
    const isLoading = ref(false);

    const passwordStrength = computed(() => {
      const password = newPassword.value;
      if (!password) return 0;

      let strength = 0;
      if (password.length >= 8) strength += 25;
      if (password.match(/[A-Z]/)) strength += 25;
      if (password.match(/[0-9]/)) strength += 25;
      if (password.match(/[^A-Za-z0-9]/)) strength += 25;

      return strength;
    });

    const strengthClass = computed(() => {
      const strength = passwordStrength.value;
      if (strength <= 25) return 'weak';
      if (strength <= 50) return 'medium';
      if (strength <= 75) return 'good';
      return 'strong';
    });

    const strengthText = computed(() => {
      const strength = passwordStrength.value;
      if (strength <= 25) return '弱';
      if (strength <= 50) return '中';
      if (strength <= 75) return '良好';
      return '强';
    });

    const isValidPassword = computed(() => {
      return passwordStrength.value >= 0;
    });

    const resetPassword = async () => {
      if (!userId.value || !resetCode.value || !newPassword.value) {
        ElMessage({
          message: '请填写所有必填项',
          type: 'warning',
          duration: 2000,
          center: true,
        });
        return;
      }

      if (!isValidPassword.value) {
        ElMessage({
          message: '密码强度不足，请确保包含大写字母、数字和特殊字符',
          type: 'warning',
          duration: 3000,
          center: true,
        });
        return;
      }

      isLoading.value = true;

      try {
        await axios.post('/backend/api/account/password/reset/', {
          id: userId.value,
          token: resetCode.value,
          new_password: newPassword.value
        });

        ElMessage({
          message: '密码重置成功！',
          type: 'success',
          duration: 2000,
          center: true,
        });

        setTimeout(() => {
          router.push('/login');
        }, 1500);

      } catch (error) {
        let errorMessage = '密码重置失败，请稍后重试';

        if (error.response && error.response.status === 400) {
          errorMessage = '重置码无效或已过期';
        }

        ElMessage({
          message: errorMessage,
          type: 'error',
          duration: 3000,
          center: true,
        });
      } finally {
        isLoading.value = false;
      }
    };

    return {
      userId,
      resetCode,
      newPassword,
      showPassword,
      isLoading,
      passwordStrength,
      strengthClass,
      strengthText,
      isValidPassword,
      resetPassword
    };
  }
};
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.form-wrapper {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(10px);
  padding: 50px;
  border-radius: 50px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}
.form-header {
  /* 添加底部间距 */
  margin-bottom: 25px;
  text-align: center;
}
.toggle-password {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.7);
  cursor: pointer;
  font-size: 14px;
  padding: 0;
}

.toggle-password:hover {
  color: #fff;
}

.password-strength {
  margin-top: -20px;
  margin-bottom: 25px;
}

.strength-bar {
  height: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 8px;
}

.strength-fill {
  height: 100%;
  transition: all 0.3s;
}

.strength-fill.weak {
  background-color: #ff4d4d;
}

.strength-fill.medium {
  background-color: #ffd700;
}

.strength-fill.good {
  background-color: #2ecc71;
}
.strength-fill.strong {
  background-color: #27ae60;
}

.strength-text {
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
  display: flex;
  justify-content: flex-end;
}

.form-footer {
  position: relative;
  margin-top: 24px;
  text-align: center;
  padding-top: 20px;
}

.form-footer::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60%;
  height: 1px;
  background: rgba(255, 255, 255, 0.1);
}

.back-link {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 14px;
  transition: 0.3s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.back-link:hover {
  color: #fff;
  transform: translateX(-5px);
}

.arrow {
  font-size: 18px;
  transition: 0.3s;
}

.back-link:hover .arrow {
  transform: translateX(-3px);
}

.input-group {
  /* 增加输入框之间的间距 */
  margin-bottom: 25px;
  position: relative;
}

/* 调整输入框标签的位置和大小 */
.input-group label {
  margin-bottom: 8px;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
}

/* 调整输入框本身的样式 */
.input-group input {
  padding: 5px 5px;
  font-size: 16px;
}

/* 修改密码强度指示器的位置 */
.password-strength {
  margin-top: 10px;
  margin-bottom: 35px;
}

/* 调整提交按钮的样式 */
.submit-btn {
  padding: 8px 10px;
  width: 60%;
}

/* 底部返回链接的位置调整 */
.form-footer {
  margin-top: 35px;
  padding-top: 25px;
}

/* 密码输入框动画效果 */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

.input-group input:invalid:focus {
  animation: shake 0.3s ease-in-out;
}

/* 加载动画 */
@keyframes pulse {
  0% { transform: scale(0.95); opacity: 0.7; }
  50% { transform: scale(1); opacity: 1; }
  100% { transform: scale(0.95); opacity: 0.7; }
}

.submit-btn:disabled {
  animation: pulse 1.5s infinite;
}

/* 响应式调整 */
@media (max-width: 480px) {
  .form-wrapper {
    padding: 30px 20px;
  }

  h2 {
    font-size: 24px;
  }

  .subtitle {
    font-size: 13px;
  }

  .input-group {
    margin-bottom: 25px;
  }

  .input-group input {
    font-size: 15px;
  }

  .input-group label {
    font-size: 15px;
  }

  .submit-btn {
    padding: 14px;
    font-size: 15px;
  }
}

/* 暗色模式支持 */
@media (prefers-color-scheme: dark) {
  .form-wrapper {
    background: rgba(0, 0, 0, 0.2);
  }

  .input-group input {
    color: #fff;
  }

  .input-group label {
    color: rgba(255, 255, 255, 0.7);
  }

  .strength-fill.weak {
    background-color: #ff6b6b;
  }

  .strength-fill.medium {
    background-color: #ffd93d;
  }
}
</style>